package com.example.beson.androiddemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beson.androiddemo.utils.CardAdapter;
import com.example.beson.androiddemo.utils.EndLessOnScrollListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    /**
     * 左滑菜单
     */
    private DrawerLayout drawer_layout;
    private TextView nav_name, nav_age,header_qq,header_wx,header_mm,header_sina,header_yy;
    /**
     * floatingactionbutton
     */
    private FloatingActionButton main_flaot_button;
    /**
     * recyclerview
     */
    private RecyclerView main_recycler;
    private CardAdapter cardAadpter;
    private List<String> list=new ArrayList<>();
    private GridLayoutManager gridManager;
    /**
     * swiperefreshlayout下拉刷新
     */
    private SwipeRefreshLayout swipe_refresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar main_toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        setSupportActionBar(main_toolbar);
        drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        /**
         * 获取navigationView headerview中空间的实例
         */
        View headerview = navigationView.getHeaderView(0);
        nav_name = (TextView) headerview.findViewById(R.id.nav_name);
        nav_name.setText("姓名：猪八戒");
        nav_age = (TextView) headerview.findViewById(R.id.nav_age);
        nav_age.setText("年龄：500岁");
        header_qq= (TextView) headerview.findViewById(R.id.header_qq);
        header_qq.setOnClickListener(this);
        header_wx= (TextView) headerview.findViewById(R.id.header_wx);
        header_wx.setOnClickListener(this);
        header_mm= (TextView) headerview.findViewById(R.id.header_mm);
        header_mm.setOnClickListener(this);
        header_sina= (TextView) headerview.findViewById(R.id.header_sina);
        header_sina.setOnClickListener(this);
        header_yy= (TextView) headerview.findViewById(R.id.header_yy);
        header_yy.setOnClickListener(this);
        /**
         * 在actionbar的左侧加一个按钮，可以点击滑出左滑菜单
         */
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.mipmap.choice_icon);
        }
        /**
         * 悬浮按钮
         */
        main_flaot_button= (FloatingActionButton) findViewById(R.id.main_flaot_button);
        main_flaot_button.setOnClickListener(this);

        /**
         * recyclerView
         */
        for(int i='A';i<'Z'+1;i++){
            list.add(""+(char)i);
        }
        main_recycler= (RecyclerView) findViewById(R.id.main_recycler);
        gridManager=new GridLayoutManager(this,2);
        main_recycler.setItemAnimator(new DefaultItemAnimator());
        main_recycler.setLayoutManager(gridManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        main_recycler.setHasFixedSize(true);
        cardAadpter=new CardAdapter(list,this);
        //添加上拉刷新
        cardAadpter.addFooterView(R.layout.load_more_layout);
        main_recycler.setAdapter(cardAadpter);
        cardAadpter.setOnItemClickLinstener(new CardAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent_detail=new Intent(MainActivity.this,DetailActivity.class);
                startActivity(intent_detail);
            }
        });
        /**
         * 上拉刷新
         */
        main_recycler.addOnScrollListener(new EndLessOnScrollListener(gridManager){

            @Override
            public void onLoadMore() {
                cardAadpter.setFooterVisible(View.VISIBLE);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        List<String> listNext=new ArrayList<>();
                        for(int i='A';i<'Z'+1;i++){
                            listNext.add(""+(char)i);
                        }
                        cardAadpter.setFooterVisible(View.GONE);
                        list.addAll(listNext);
                        cardAadpter.notifyDataSetChanged();
                    }
                },2000);
            }
        });
        /**
         * 下拉刷新
         */
        swipe_refresh= (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
//        swipe_refresh.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        swipe_refresh.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        list.clear();
                        for(int i='A';i<'Z'+1;i++){
                            list.add(""+(char)i);
                        }
                        cardAadpter.notifyDataSetChanged();
                        swipe_refresh.setRefreshing(false);
                    }
                },2000);
            }
        });
    }

    /**
     * 初始化Menu菜单
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    /**
     * 给toolbar上mune添加点击事件
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.main_add:
                Intent intent_add=new Intent(MainActivity.this,TabButtonActivity.class);
                startActivity(intent_add);
                break;
            case R.id.main_delete:
                Intent intent_scroll=new Intent(MainActivity.this,ScrollViewTestActivity.class);
                startActivity(intent_scroll);
                break;
            case R.id.main_choice:
                Intent intent_choice=new Intent(MainActivity.this,ScanCardActivity.class);
                startActivity(intent_choice);
                break;
            case android.R.id.home:
                drawer_layout.openDrawer(GravityCompat.START);
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_qq:
                drawer_layout.closeDrawers();
                Intent login_mvc=new Intent(MainActivity.this,MVCLoginActivity.class);
                startActivity(login_mvc);
                break;
            case R.id.header_wx:
                drawer_layout.closeDrawers();
                Intent login_mvp=new Intent(MainActivity.this,MVPLoginActivity.class);
                startActivity(login_mvp);
                break;
            case R.id.header_mm:
                Intent intent_main=new Intent(MainActivity.this,MainFragmentActivity.class);
                startActivity(intent_main);
                drawer_layout.closeDrawers();
                break;
            case R.id.header_sina:
                Intent intent_view=new Intent(MainActivity.this,ToolBarAndViewPagerActivity.class);
                startActivity(intent_view);
                drawer_layout.closeDrawers();
                break;
            case R.id.header_yy:
                Intent intent_glide=new Intent(MainActivity.this,GlideNoThumActivity.class);
                startActivity(intent_glide);
                drawer_layout.closeDrawers();
                break;
            case R.id.main_flaot_button:
                //snackbar也是用来提示用户的，和Toast像是，但是上面可以有按钮，比如删除时可以添加一个取消的按钮
                Snackbar.make(v,"确认删除？",Snackbar.LENGTH_SHORT).setAction("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "snackbar也是用来提示用户的，和Toast像是，但是上面可以有按钮，比如删除时可以添加一个取消的按钮", Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;
            default:
                break;
        }
    }
}
